import { constantRoutes, dynamicRoutes } from '@/router'
const state = {
  routes: []
}

const mutations = {
  setRoutes(state, data) {
    state.routes = [...constantRoutes, ...data]
  }
}

const actions = {
  routesFilter(context, roles) {
    // 根据用户的权限配置路由
    const personalRoutes = dynamicRoutes.filter(item => roles.menus.indexOf(item.name) > -1)
    personalRoutes.push(
      // 404 page must be placed at the end !!!
      { path: '*', redirect: '/404', hidden: true }
    )
    context.commit('setRoutes', personalRoutes)
    return personalRoutes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
